<?php
/**
 * Description of FrontendBaseController
 *
 * @author Alexey
 */

Yii::import("application.controllers.SiteBaseController");

class FrontendBaseController extends SiteBaseController {

    public $menu=array();
    public $breadcrumbs=array("asd");

    public function setBreadcrumbs($arr)
    {
        $this->breadcrumbs = $arr;
    }

    public function beforeAction($action)
    {
        Yii::app()->clientScript->registerCoreScript('jquery');
        Yii::app()->clientScript->registerScriptFile('/js/library.js');
        return parent::beforeAction($action);
    }

    public function actionLogin()
    {
        $model = new UserLoginForm;        

        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='user-login-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if(isset($_POST['UserLoginForm']))
        {            
            if (isset($_POST['UserLoginForm']['rememberMe']) && $_POST['UserLoginForm']['rememberMe'] === "on")
            {
                $_POST['UserLoginForm']['rememberMe'] = true;
            }            
            $model->attributes=$_POST['UserLoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect($this->redirectToUserPage());
        }

        $this->breadcrumbs = array( "Главная" => "/",
                            "Вход" => "",
                        );

        $this->render('login', array('userLoginForm'=>$model, 'leftLoginDisable' => 1));
    }

    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect("/");
    }

    public function redirectToUserPage()
    {
        $url = $this->getUserMainPageUrl();
        $this->redirect($url);
    }

    public function getUserMainPageUrl($id = null)
    {
        if (isset($id))
        {
            return "/user/" . $id;
        }
        else
        {
            return "/user/" . $this->getUserIdFromSession();
        }
    }

    public function getUserPageUrl($id)
    {
        return "/user/" . $id;
    }

    public function getPetPageUrl($id)
    {
        return "/pet/view/" . $id;
    }

    public function getUserIdFromSession()
    {
        return Yii::app()->user->id;
    }

    public function getUserFromSession()
    {
        return UserFacade::getUserById(Yii::app()->user->id);
    }
    
    public function checkDirExists($path)
    {
        if(!is_dir($path))
        {                    
            mkdir($path);
        }
    }
    
    public function isAuthor($user)
    {
        if ($user != null)
        {
            if ($this->getUserIdFromSession() == $user->user_id)
            {
                return true;
            }
        }
        return false;
    }

    public function isOwner()
    {
        if (Yii::app()->request->getParam("user"))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    protected function registerAjaxSearch($searchBlockId, $listBlockId)
    {
        Yii::app()->clientScript->registerScript('search',
           "var ajaxUpdateTimeout;
            var ajaxRequest;
            $('#" . $searchBlockId . " input').keydown(function(ev){
                if (ev.keyCode == 13) {
                    ev.preventDefault();
                    return false;
                }
            });
            $('#" . $searchBlockId . " input').keyup(function(ev){
                if (ev.keyCode == 13) {
                    ev.preventDefault();
                    return false;
                }
                ajaxRequest = $(this).serialize();
                clearTimeout(ajaxUpdateTimeout);
                ajaxUpdateTimeout = setTimeout(function () {
                    $.fn.yiiListView.update(
                        '" . $listBlockId . "',
                        {data: ajaxRequest}
                    )
                },
                500);
            });"
        );
    }


}